//
//  DGDownloadTask.m
//  DGDownloader
//
//  Created by Jinxiao on 4/15/16.
//  Copyright © 2016 Codoon. All rights reserved.
//

#import "DGDownloadTask.h"

#import <CommonCrypto/CommonDigest.h>

#import "DGDownloadManager.h"

@implementation DGDownloadTask

- (NSString *)identifier {
    return [_sourceURL.absoluteString identifierForDownloadTask];
}

- (void)start {
    [[DGDownloadManager sharedInstance] enqueueDownloadTask:self];
}

- (NSString *)filePath {
    return [NSTemporaryDirectory() stringByAppendingPathComponent:self.identifier];
}

@end

@implementation NSString (CDDownloadTask)

- (NSString *)identifierForDownloadTask {
    const char *str = self.UTF8String;
    unsigned char r[16];
    CC_MD5(str, (uint32_t)strlen(str), r);

    return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
}

@end
